package com.banzhi.shop.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.banzhi.shop.R;
import com.banzhi.shop.activity.OrderDetailsActivity;
import com.banzhi.shop.activity.ShouhouOrderInfoActivity;
import com.banzhi.shop.adapter.CommonAdapter;
import com.banzhi.shop.entity.shouyijilu.Shouyi_Data;
import com.banzhi.shop.model.CallBack.ShouyejiluCallBack;
import com.banzhi.shop.model.IShouyejiluModel;
import com.banzhi.shop.model.Impl.ShouyijiluImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class Shouyi_YifaFragment extends Fragment implements ShouyejiluCallBack, SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private IShouyejiluModel model;
    private int page = 1;
    private List<Shouyi_Data> shouyidata = new ArrayList<>();
    private CommonAdapter<Shouyi_Data> adapter;
    private LinearLayoutManager LayoutManager;
    private RelativeLayout rlnodata;
    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_shouyi__yifa, container, false);
        setinits(view);
        setlisteners();
        model = new ShouyijiluImpl();
        LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
        model.getData(page, "2", this);
        return view;
    }

    public void setinits(View view) {
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.srl_listview);
        refreshLayout.setColorSchemeResources(R.color.themeColor);
        refreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        LayoutManager = new LinearLayoutManager(getActivity());
        LayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(LayoutManager);
        rlnodata = (RelativeLayout) view.findViewById(R.id.rl_nodata);
    }

    private void setlisteners() {
        refreshLayout.setOnRefreshListener(this);

        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(LayoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
                model.getData(page, "2", Shouyi_YifaFragment.this);
            }
        });
    }


    @Override
    public void onRefresh() {
        LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
        page = 1;
        model.getData(page, "2", this);
    }

    /**
     * @param object 获取收益记录成功
     */
    @Override
    public void ongetDataSuccess(Object object) {
        final List<Shouyi_Data> data = (List<Shouyi_Data>) object;
        if (page == 1) {
            shouyidata.clear();
        } else {
            if (data.size() == 0) {
                return;
            }
        }
        if (data.size() > 0) {
            shouyidata.addAll(data);
        }

        if (shouyidata.size() == 0) {
            rlnodata.setVisibility(View.VISIBLE);
        } else {
            rlnodata.setVisibility(View.GONE);
        }
        if (adapter == null) {
            adapter = new CommonAdapter<Shouyi_Data>(getActivity(), R.layout.item_daifa_shouyi, shouyidata) {
                @Override
                public void convert(CommonAdapter.ViewHolder holder, final Shouyi_Data data, int position) {
                    holder.setText(R.id.tv_ordernum, data.getOrdersn());
                    holder.getView(R.id.tv_ordernum).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (Integer.parseInt(data.getRefundstate()) > 0) {
                                Intent intent = new Intent(getContext(), ShouhouOrderInfoActivity.class);
                                intent.putExtra("id", data.getRefundid());
                                intent.putExtra("shoperOrder", data.getShoperOrder());
                                getContext().startActivity(intent);
                            } else {
                                Intent osintent = new Intent(getContext(), OrderDetailsActivity.class);
                                osintent.putExtra("id", data.getOrderid());
                                osintent.putExtra("status", data.getStatus2());
                                osintent.putExtra("shoperOrder", data.getShoperOrder());
                                getContext().startActivity(osintent);
                            }
                        }
                    });
                    holder.getTextView(R.id.tv_fuzhi).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ShareUtils.copyLianjie(getActivity(), data.getOrdersn());
                        }
                    });
                    holder.setText(R.id.tv_title, data.getTitle());
                    holder.setText(R.id.tv_tuijian, data.getAgent());
                    holder.setText(R.id.tv_xiaofei, data.getMember());
                    holder.setText(R.id.tv_liruntitle1, data.getContent() + " ：");
                    if (ASUtils.isCanUse(data.getMoney())) {
                        holder.setText(R.id.tv_lirun1, NumberUtils.DecimalFormat(data.getMoney()));
                    }
                    if (!ASUtils.isCanUse(data.getMoney2())) {
                        holder.getView(R.id.ll_lirun2).setVisibility(View.GONE);
                    } else {
                        holder.getView(R.id.ll_lirun2).setVisibility(View.VISIBLE);
                        holder.setText(R.id.tv_liruntitle2, data.getContent2() + " ：");
                        if (ASUtils.isCanUse(data.getMoney2())) {
                            holder.setText(R.id.tv_lirun2, NumberUtils.DecimalFormat(data.getMoney2()));
                        }
                    }
                    if (ASUtils.isCanUse(data.getCreatetime())) {
                        holder.setText(R.id.tv_time, DateUtil.getData(data.getCreatetime()));
                    }
                    holder.setText(R.id.tv_statu, data.getStatus());
                }
            };
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
    }

    /**
     * @param object 获取收益记录失败
     */
    @Override
    public void ongetDataError(Object object) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(getActivity(), jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }


}
